package com.example.communitypro.util;

import com.example.communitypro.mapper.PostMapper;
import com.example.communitypro.mapper.UserCollectionMapper;
import com.example.communitypro.mapper.UserLikesMapper;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LikeAndCollecionUtil {



    public static List<Integer> like(List<Post> posts,int user_no){
        ApplicationContext ctx = SpringUtil.getApplicationContext();
        UserLikesMapper userLikesMapper = (UserLikesMapper) ctx.getBean("userLikesMapper");
        List<Post> posts1 = userLikesMapper.getPostLike(user_no);//用户点赞的帖子
        List<Integer> postNos = new ArrayList<>();
        for ( Post post : posts) {
            for ( Post post1 : posts1 ){
                if (post.getPostNo().equals(post1.getPostNo())) {
                    postNos.add(post1.getPostNo());
                    break;
                }
            }
        }
        return postNos;
    }

    public static List<Integer> collecion(List<Post> posts,int user_no){
        ApplicationContext ctx = SpringUtil.getApplicationContext();
        UserCollectionMapper userCollectionMapper = (UserCollectionMapper) ctx.getBean("userCollectionMapper");
        List<Post> posts1 = userCollectionMapper.getPostCollection(user_no);//用户收藏的帖子
        List<Integer> postNos = new ArrayList<>();
        for (Post post : posts) {
            for (Post post1 : posts1){
                if (post.getPostNo().equals(post1.getPostNo())) {
                    postNos.add(post1.getPostNo());
                    break;
                }
            }
        }
        return postNos;
    }

    public static List<Integer> like1(List<Comment> comments, int user_no){
        ApplicationContext ctx = SpringUtil.getApplicationContext();
        UserLikesMapper userLikesMapper = (UserLikesMapper) ctx.getBean("userLikesMapper");
        List<Comment> comments1 = userLikesMapper.getCommentLike(user_no);//用户点赞的评论
        List<Integer> commentsNo = new ArrayList<>();
        for ( Comment comment : comments) {
            for ( Comment comment1 : comments1 ){
                if (comment.getCommentNo().equals(comment1.getCommentNo())) {
                    commentsNo.add(comment.getCommentNo());
                    break;
                }
            }
        }
        return commentsNo;
    }


    public static List<Integer> collecion1(List<Comment> comments,int user_no){
        ApplicationContext ctx = SpringUtil.getApplicationContext();
        UserCollectionMapper userCollectionMapper = (UserCollectionMapper) ctx.getBean("userCollectionMapper");
        List<Comment> comments1 = userCollectionMapper.getCommentCollection(user_no);//用户收藏的评论
        List<Integer> commentsNo = new ArrayList<>();
        for ( Comment comment : comments) {
            for ( Comment comment1 : comments1 ){
                if (comment.getCommentNo().equals(comment1.getCommentNo())) {
                    commentsNo.add(comment.getCommentNo());
                    break;
                }
            }
        }
        return commentsNo;
    }
}
